﻿using System;
using Rabbit.Logic;
using RabbitShop.Code;

namespace RabbitShop.Action
{
    public partial class OrderShipCompany : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string uid = string.Empty;
            if (Request.Cookies[Constant.CookieName] != null)
            {
                uid = Request.Cookies[Constant.CookieName].Values[Constant.CookieUid];
                if (string.IsNullOrEmpty(uid)) Response.Redirect("~/UI/UserLogon.aspx");
            }
            else
            {
                Response.Redirect("~/UI/UserLogon.aspx");
            }

            string aid = Request["aid"];
            if (string.IsNullOrEmpty(aid))
            {
                Response.Write("false");
                return;
            }

            int index;
            if (!int.TryParse(aid, out index))
            {
                Response.Write("false");
                return;
                
            }

            Customer customer = RabbitUse.GetCustomerById(Db.Store, uid);
            if (customer.PreOrderShopItem == null)
            {
                Response.Write("false");
                return;
            }

            customer.PreOrderShopItem.ShipCompanyIndex = index;
            RabbitUse.AddOrUpdateCustomer(Db.Store, customer);
            Response.Write("true");
        }
    }
}